<template>
  <div class="app-container">
    <search-bar :form="queryParams" @sub="getList" class="seach"></search-bar>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button @click="handleAdd" class="asset">新增单据</el-button>
        <el-button plain @click="translation">打印</el-button>
      </el-col>
      <right-toolbar @queryTable="Refresh"></right-toolbar>
    </el-row>

    <vxe-table :data="tableData.list" :loading="tableData.loading"
      :pagination-config="{ pageSize: tableData.paging.pageSize, currentPage: tableData.paging.pageNum }"
      @pagination="getList" border>
      <vxe-column field="number" title="编号" width="271">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small" @click="handleDetail(row.id)">
            {{ row.number }}
          </el-button>
        </template>
      </vxe-column>
      <vxe-column field="num" title="数量" width="80"></vxe-column>
      <vxe-column field="createUserName" title="申请人" width="271"></vxe-column>
      <vxe-column field="remark" title="备注"></vxe-column>
      <vxe-column field="status" title="状态" align="center" width="271">
        <template #default="{ row }">
          <dict-tag :options="apply_state" :value="row.state" />
        </template>
      </vxe-column>
      <vxe-column title="操作" align="center" width="180">
        <template #default="{ row }">
          <el-button v-if="row.state === 0 || row.state === 3" style="margin-right: 10px;" link type="primary"
            size="small" @click="handleSub(row)">提交</el-button>
          <el-button v-if="row.state === 0 || row.state === 3" style="margin-right: 10px;" link type="primary"
            size="small" @click="handleUpdate(row)">修改</el-button>
          <el-button v-if="row.state === 0 || row.state === 3" size="small" link type="primary"
            @click="handleDelete(row)">删除</el-button>
          <el-button v-if="row.state !== 0 && row.state !== 3" size="small" link type="primary"
                     @click="handleDetail(row.id)">审批详情</el-button>
          <el-button v-if="row.state !== 0 && row.state !== 3" size="small" link type="primary"
                     @click="formPrintOne(row)">打印</el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <div class="pagination-container">
      <pagination v-show="tableData.paging.total > 0" :total="tableData.paging.total"
        v-model:page="tableData.paging.pageNum" v-model:limit="tableData.paging.pageSize" @pagination="getList" />
    </div>
    <el-dialog v-model="isRestDialogVisible" width="50%">
      <rest-dialog ref="RestDialog" @sub="getList"></rest-dialog>
    </el-dialog>
    <edit :apply-type="applyType" ref="edit" @sub="handleAddSuccess"></edit>
    <detail ref="detailDialog"></detail>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import api from "./api.js";
import SearchBar from "./SearchBar.vue";
import Edit from './edit.vue';
import Detail from "./detail/index.vue";
import RestDialog from "./Restitution.vue";

const edit = ref();
const detailDialog = ref();

const { proxy } = getCurrentInstance();
const { apply_state } = proxy.useDict("apply_state");

// 申请类型
const applyType = ref('ZCGH');

const isRestDialogVisible = ref(false);

const queryParams = reactive({
  applyType: applyType.value,
});

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

function getList() {
  tableData.loading = true;
  const { pageNum, pageSize } = tableData.paging;

  const query = { ...queryParams };
  const pageReq = { page: pageNum - 1, size: pageSize };
  api.list(query, pageReq).then(response => {
    const { content, totalElements } = response;
    if (content && content.length > 0) {
      content.forEach(item => {
        if (item.formData) {
          let formData = JSON.parse(item.formData);
          Object.assign(item, formData);
        }
      });
    }

    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;
   
  });
}

/** 新增按钮操作 */
function handleAdd() {
  edit.value.open();
}

function translation() {
  isRestDialogVisible.value = true;
}
/** 新增操作成功后的回调 */
function handleAddSuccess() {
  // 新增成功后的处理
  proxy.$message.success("新增成功");
  getList();  // 刷新数据
}

/** 修改按钮操作 */
function handleUpdate(row) {
  edit.value.open(row);
}

function handleDetail(row) {
  detailDialog.value.open(row);
}

function handleSub(row) {
  api.sub(row.id).then(() => {
    getList();
  });
}
function Refresh() {
  proxy.$message.success("刷新成功");
}

/** 删除按钮操作 */
function handleDelete(row) {
  this.$modal.confirm('是否确认删除').then(() => {
    return api.del(row.id);
  }).then(() => {
    getList();
    this.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

onMounted(() => {
  getList();
});

</script>
<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh;
  padding: 9px;
  overflow: hidden;
}
.vxe-table {
  flex: 1;
  overflow: auto;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;

}
.asset {
  width: 74px;
  height: 35px;
  background-color: #005bf5;
  color: #fff;
  font-size: 12px;
  border-radius: 8%;
  padding: 0;
}
</style>
